#include "fcn_tables.h"
//---------------------------------------------------------------------------

FnTable* FnTable::self = 0L;
int FnTable::Pow2PowTable[81][1001];
int FnTable::Exp255Table[3001];
int FnTable::Exp5Table[1001];
int FnTable::Asin255kTable[20001];
//int FnTable::PowTextTable[5][5];

FnTable::FnTable()
{
    Pow2PowInit();
    Exp255Init();
    Exp5Init();
    Asin255kInit();
//	PowTextInit(3, 0.2);
}

void FnTable::Pow2PowInit()
{
    int N = 1000;
    int Ny = 80;

    for (int j=0; j<=Ny; j++)
    {
        float y = (j-30)*0.1;

        for (int i=0; i<=N; i++)
        {
            float x = i / (float)N;
            int f = 3000 * pow(x, pow(2.0, y));
            Pow2PowTable[j][i] = f;
        }
    }
}
//---------------------------------------------------------------------------

void FnTable::Exp255Init()
{
    int N = 3000;

    for (int i=0; i<=N; i++)
    {
        float x = i / (float)N;
        int y = 255*exp(-(1-x)*10);
        Exp255Table[i] = y;
    }
}
//---------------------------------------------------------------------------

void FnTable::Exp5Init()
{
    int N = 1000;

    for (int i=0; i<=N; i++)
    {
        float x = (i / (float)N);
        int f = 255*exp(-x*5);
        Exp5Table[i] = f;
    }
}
//---------------------------------------------------------------------------

void FnTable::Asin255kInit()
{
    int N = 20000;
    for (int i=0; i<=N; i++)
    {
        float x = sqrt(i / ((float)N));
        int f = 0;
        if (x != 0.0)
            f = asin(x) / x * 255;
        Asin255kTable[i] = f;
    }
}
//---------------------------------------------------------------------------

int FnTable::isqrt(unsigned long x)
{
    unsigned long rem = 0;
    unsigned long root = 0;
    for (int i=0; i<16; i++)
    {
        root <<= 1;
        rem = ((rem << 2) + (x >> 30));
        x <<= 2;
        root++;
        if (root <= rem)
        {
            rem -= root;
            root++;
        }
        else
            root--;
    }
    return (unsigned short)(root >> 1);
}
//---------------------------------------------------------------------------

//void FnTable::PowTextInit(int blur, float power)
//{
//	for (int i=0; i<=blur; i++)
//	{
//		for (int j=0; j<=blur; j++)
//		{
//			float dx = i/(float)blur;
//			float dy = j/(float)blur;
//			float k = 1 - pow((dx*dx + dy*dy)/2, power);
//			PowTextTable[i][j] = k*255;
//        }
//    }
//}
//---------------------------------------------------------------------------

